var aFunc = {
	takePhoto:function(callback){
			plus.camera.getCamera().captureImage(function(p) {
			plus.io.resolveLocalFileSystemURL(p, function(entry) {
				aFunc._doCompress4Photo(entry.toLocalURL(),function(url){
					var files = [];
					files.push({ 
						name: "uploadkey-" + new Date(),
						path: url,
						type:"rent"
					});
					FileUploader.upload(files, function(data) {
						console.log(data);
						callback(data)
					});
					
				});
			
			}, function(e) {
				mui.toast("读取拍照文件错误：" + e.message);
			});
		}, function(e) {
			console.log(e.message);
			mui.toast("失败：" + e.message);
		}, {
			index: 1
		});
	},
	selectPhoto:function(callback){
		plus.gallery.pick(function(p) {
		plus.io.resolveLocalFileSystemURL(p, function(entry) {
			aFunc._doCompress4Photo(entry.toLocalURL(),function(url){
					var files = [];
					files.push({ 
						name: "uploadkey-" + new Date(),
						path: url,
						type:"rent"
					});
					FileUploader.upload(files, function(data) {
						console.log(data);
						callback(data)
					});
					
			});
		}, function(e) {
			mui.toast("读取相册文件错误：" + e.message);
		})
		});
	},
	bindEvent : function(){
	/**
	 *选择图片 
	 */
	aVariable.btn.addPhoto.addEventListener('tap',function(event){
		var callback = function(data) {
			console.log(data);
			for(var i=0;i<data.length;i++){
				aVariable.params.images.push(data[i].value);
				var li = document.createElement("li");
				li.className = "mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3";
				li.innerHTML= '<img class="mui-media-object" src="' + data[i].text + '" />';
				//console.log(li.innerHTML);
				aVariable.box.boxPhoto.insertBefore(li,aVariable.btn.addPhoto);
			}
		};
		var btnArray = [{
			title: "拍照"
		}, {
			title: "相册"
		}];
		plus.nativeUI.actionSheet({
				title: "更换照片",
				cancel: "取消",
				buttons: btnArray
				}, function(e) {
					switch (e.index) {
						case 0: // 点击取消
							return;
						case 1: // 点击拍照
							aFunc.takePhoto(callback);
							return;
						case 2: // 点击相册
							aFunc.selectPhoto(callback);
							return;
					}
				});
	},false);
		/***
		 *提交数据 
		 */
		aVariable.btn.btnConfirm.addEventListener('tap',function(event){
			var type="0";
			for(var i=0;aVariable.ipt.iptType.length;i++){
				if(aVariable.ipt.iptType[i].checked){
					type=aVariable.ipt.iptType[i].value;
					break;
				}
			}
			var description=aVariable.ipt.iptDescription.value;
			var title=aVariable.ipt.iptRentTitle.value;
			var imagUrl="";
			for(var i=0;i<aVariable.params.images.length;i++){
				imagUrl+=aVariable.params.images[i]+",";
			}
			var account=LocalStorage.getItem(LocalStorage.keys.User_Account);
			mySever.addHouseRent(account,title,type,description,imagUrl,function(data){
					mui.toast("发布信息成功");
					mui.back();
				},function(){
					mui.toast("保存数据失败");
				});
			
		},false);
		

	},
	_doCompress4Photo:function(photoLocalUrl, callback) {
		var options = {
			overwrite: true,
			quality: 50,
			src: photoLocalUrl,
			dst: photoLocalUrl
		}; 
		plus.zip.compressImage(options,
			function(event) {
				var target = event.target; // 压缩转换后的图片url路径，以"file://"开头
				callback(target);
			},
			function(error) {
				mui.toast("图片压缩处理失败");
			}
		);
	},
	
	plusReady: function() {
		if (mui.os.plus) {
			plus.navigator.closeSplashscreen();
			plus.screen.lockOrientation("portrait-primary");
			aVariable.webview.current = plus.webview.currentWebview();
		}
		aVariable.params.images=new Array();
		// 绑定事件
		aFunc.bindEvent();
	}
};